using System;
using System.Threading;
using System.Web.Mvc;
using Yonkly.Models;

namespace Yonkly.Fitlers
{
    public class ErrorFilter : ActionFilterAttribute
    {

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Exception != null && !filterContext.ExceptionHandled)
            {
                if (filterContext.Exception.GetType() == typeof(ThreadAbortException))
                {
                    return;
                }
                try
                {
                    filterContext.HttpContext.Session["lasterror"] = filterContext.Exception;
                    filterContext.HttpContext.Response.Redirect("/error.aspx");
                }
                catch
                {
                    //ignore this exception otherwise we will go into an infinite loop
                }
                finally
                {
                    filterContext.ExceptionHandled = true;
                }
            }
        }
    }
}